﻿using SmartConference.Api.Applocations.IServices;
using SmartConference.Domain;
using SmartConference.ErrorCode;
using SmartConference.Infrastructure.Interfaces;

namespace SmartConference.Api.Applocations.Services
{
    public class MeetingApprovalServices : IMeetingApprovalServices
    {
        private readonly IMeetingApprovalRepository meetingApprovalRepository;

        public MeetingApprovalServices(IMeetingApprovalRepository meetingApprovalRepository)
        {
            this.meetingApprovalRepository = meetingApprovalRepository;
        }

        public async Task<ApiResult> ApprovalMeeting(int id, int state, string? notes)
        {
            try
            {
                var result =await meetingApprovalRepository.GetAsync(x => x.MeetApprovalId == id);
                if (result != null)
                {
                    result.Result = (ApprovalResult)state;
                    result.Status=ApprovalState.已审批;
                    result.ApprovalNotes = notes;
                    await meetingApprovalRepository.UpdateAsync(result);
                    return ApiResult.Success(ApiResultCode.Success);
                }
                return ApiResult.Error(ApiResultCode.Error, "审批失败");
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<ApiResult<List<MeetingApprove>>> ShowApprove(int state, string? MeetingTheme, string? RoomName)
        {
            try
            {
                var list = meetingApprovalRepository.GetAll(x=>x.Status==(ApprovalState)state);
                if (!string.IsNullOrEmpty(MeetingTheme))
                {
                    list = list.Where(x => x.MeetingTheme.Contains(MeetingTheme));
                }
                if (!string.IsNullOrEmpty(RoomName))
                {
                    list = list.Where(x => x.RoomName.Contains(RoomName));
                }
                return ApiResult<List<MeetingApprove>>.Success(ApiResultCode.Success, list.ToList());

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
